import { _decorator, assetManager, Component, ImageAsset, Sprite, SpriteFrame, } from 'cc';
import { BGPFrame } from './components'
import { Logger, fitCover } from './utils';
import { RootStore } from './mixin';

const { ccclass, property, uniquelyReferenced } = _decorator;

@uniquelyReferenced
@ccclass('Main')
export class Main extends Component {

    @property({
        tooltip: '背景图帧',
        displayName: '背景图帧',
        type: BGPFrame,
    })
    bgpFrame: BGPFrame

    store: RootStore = RootStore.instance

    #log = new Logger(this.name)

    showBGP(content: LDContent.BGPContent) {
        this.#log.debug('@showBGP()', content)
        const resource = this.store.getResource(content.resourceId)
        assetManager.loadRemote<ImageAsset>(resource.ossUrl, (err, imageAsset) => {
            if (err) {
                this.#log.error('@showBGP() error', err.message)
                return
            }
            this.#log.debug('@showBGP() imageAsset', imageAsset)
            this.bgpFrame.getComponent(Sprite).spriteFrame = SpriteFrame.createWithImage(imageAsset)
            fitCover(imageAsset, this.bgpFrame)
        })
    }
}


